import { Im } from './lim-js-sdk';
import store from '../store';
import router from '../router'
import { Message } from 'element-ui'

const LIM = new Im();

const lychat = LIM.create({
    url: "ws://127.0.0.1:8811/im"
});
lychat.on(Im.EVENT.READY, () => {
    console.log('sdk ready success');
});
lychat.on(Im.EVENT.CLOSE, () => {
    Message({
        showClose: true,
        message: '连接波动，请您重新连接',
        duration: 0,
        onClose: () => {
            router.go(0)
        },
        type: 'error'
    });
});
lychat.on(Im.EVENT.NOT_READY, () => {
    Message({
        showClose: true,
        message: '您已掉线，请重新登录',
        duration: 0,
        onClose: () => {
            router.go(0)
        },
        type: 'error'
    });
});
lychat.on(Im.EVENT.SESSION_LIST_REFRESH, (data) => {
    console.log(data);
    store.commit('addSession', data);
});
lychat.on(Im.EVENT.MESSAGE_RECEIVED, (data) => {
    console.log(data);
    store.commit('addMessage', data);
    if (data.msgFlow === 'IN') {
        messagePromptTone();
    }
});

function messagePromptTone() {
    let messagePromptTone = document.getElementById("messagePromptTone");
    // 播放提示音
    messagePromptTone.pause();
    messagePromptTone.play();
}

export default lychat